package com.burstly.lib.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import com.burstly.lib.util.LoggerExt;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class SdCardWatchingService
{
  private static final List<IWatcherNotificator> LISTENERS = new ArrayList();
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "SdCardWatchingService";
  private static SdCardWatchingService sInstance;
  private final Context mApplicationContext;
  private BroadcastReceiver mExternalStorageReceiver;
  boolean mExternalStorageUsable = false;

  private SdCardWatchingService(Context paramContext)
  {
    this.mApplicationContext = paramContext.getApplicationContext();
    startWatchingExternalStorage();
  }

  public static void addNotificationListener(IWatcherNotificator paramIWatcherNotificator)
  {
    if (paramIWatcherNotificator != null)
    {
      LISTENERS.add(paramIWatcherNotificator);
      return;
    }
    LOG.logError("SdCardWatchingService", "Notification listener is NULL. Can not add.", new Object[0]);
  }

  private void notifyListeners()
  {
    if (!LISTENERS.isEmpty())
    {
      Iterator localIterator = LISTENERS.iterator();
      while (localIterator.hasNext())
        ((IWatcherNotificator)localIterator.next()).notifySdCardStateChanged(this.mExternalStorageUsable);
    }
  }

  public static void removeNotificationListener(IWatcherNotificator paramIWatcherNotificator)
  {
    if (paramIWatcherNotificator != null)
    {
      LISTENERS.remove(paramIWatcherNotificator);
      return;
    }
    LOG.logError("SdCardWatchingService", "Notification listener is NULL. Can not remove.", new Object[0]);
  }

  public static void startWatching(Context paramContext)
  {
    try
    {
      if (sInstance == null)
      {
        LOG.logDebug("SdCardWatchingService", "Start watching after SD card state.", new Object[0]);
        sInstance = new SdCardWatchingService(paramContext);
      }
      while (true)
      {
        return;
        LOG.logDebug("SdCardWatchingService", "Watching is in progress. Ok.", new Object[0]);
      }
    }
    finally
    {
    }
  }

  private void startWatchingExternalStorage()
  {
    this.mExternalStorageReceiver = new BroadcastReceiver()
    {
      public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
      {
        LoggerExt localLoggerExt = SdCardWatchingService.LOG;
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = paramAnonymousIntent.getAction();
        localLoggerExt.logInfo("SdCardWatchingService", "Storage: {0}", arrayOfObject);
        SdCardWatchingService.this.updateExternalStorageState();
      }
    };
    IntentFilter localIntentFilter = new IntentFilter();
    localIntentFilter.addDataScheme("file");
    localIntentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
    localIntentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
    this.mApplicationContext.registerReceiver(this.mExternalStorageReceiver, new IntentFilter(localIntentFilter));
    updateExternalStorageState();
  }

  public static void stopWatching()
  {
    try
    {
      if (sInstance != null)
      {
        LOG.logDebug("SdCardWatchingService", "Stop watching SD card status.", new Object[0]);
        sInstance.stopWatchingExternalStorage();
        LISTENERS.clear();
        sInstance = null;
      }
      while (true)
      {
        return;
        LOG.logDebug("SdCardWatchingService", "Watch has already been stopped. Skip call.", new Object[0]);
      }
    }
    finally
    {
    }
  }

  private void stopWatchingExternalStorage()
  {
    this.mApplicationContext.unregisterReceiver(this.mExternalStorageReceiver);
  }

  void updateExternalStorageState()
  {
    String str = Environment.getExternalStorageState();
    this.mExternalStorageUsable = false;
    if ("mounted".equals(str))
    {
      File localFile = Environment.getExternalStorageDirectory();
      boolean bool1 = localFile.canRead();
      boolean bool2 = false;
      if (bool1)
      {
        boolean bool3 = localFile.canWrite();
        bool2 = false;
        if (bool3)
          bool2 = true;
      }
      this.mExternalStorageUsable = bool2;
    }
    notifyListeners();
  }

  public static abstract interface IWatcherNotificator
  {
    public abstract void notifySdCardStateChanged(boolean paramBoolean);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.service.SdCardWatchingService
 * JD-Core Version:    0.6.2
 */